package yag.battle.manager;

import yag.battle.factory.HorsemanFactory;
import yag.battle.factory.SwordmanFactory;
import yag.battle.model.Army;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import yag.battle.pet.manager.PetManager;
import yag.battle.pet.model.Pet;
import yag.battle.pet.model.PetType;
import yag.battle.service.ArmyService;

/**
 * Created by user on 25.08.2014.
 */
@Component
public class ArmyManager {
    @Autowired
    private SwordmanFactory swordmanFactory;
    @Autowired
    private HorsemanFactory horsemanFactory;
    @Autowired
    private ArmyService armyService;
    @Autowired
    private PetManager petManager;
    @Autowired
    private ApplicationContext applicationContext;

    public void createArmy(int id, int countSword, int countHorse, Pet pet) {
        Army army = applicationContext.getBean(Army.class);
        army.setSwordMans(swordmanFactory.createSwordmans(countSword));
        army.setHorsemans(horsemanFactory.createHorsemans(countHorse));
        army.setPet(pet);
        army.setId(id);
        Pet pet1 = null;
        try {
            pet1 = petManager.getPetByUserId(id);
        } catch (Exception e) {

        }
        if ((pet1 == null) && (pet != null)) {
            petManager.setPetUserId(pet.getId(), id);
            army.setPet(pet);
        } else if ((pet1 != null) && (pet != null)) {
            petManager.setPetUserId(pet.getId(), id);
            petManager.deleteUserIdById(pet1.getId());
            army.setPet(pet);
        } else {
            army.setPet(pet1);
        }

        armyService.addArmy(army);
    }

    public Army getArmy(int id) {
        return armyService.getArmy(id);
    }


}
